package com.shop.controller;

import com.shop.annotation.RequiresPermissionsDesc;
import com.shop.pojo.*;
import com.shop.service.DtsAdminService;
import com.shop.service.DtsBrandService;
import com.shop.service.DtsCategoryService;
import com.shop.util.ResponseUtil;
import com.shop.vo.CatVo;
import com.shop.vo.CategoryAssist;
import com.shop.vo.DtsBrandVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AdminBrandController
 * @Author xiao宋
 * @Date 19:14  2022/10/16
 * @Version 1.0
 * @Description TODO
 */
@Slf4j
@RestController
@RequestMapping("/admin/brand")
public class AdminBrandController {
    @Autowired
    DtsBrandService brandService;
    @Autowired
    DtsAdminService adminService;
    @Autowired
    DtsCategoryService categoryService;

    /**
     * 品牌和管理员列表
     *
     * @return
     */
    @GetMapping("/catAndAdmin")
    public Object catAndAdmin() {
        /**
         *      封装第一个数据部分
         */
        List<CategoryAssist> adminList = adminService.findAllAdmin();

        /**
         *      封装第二个数据部分
         */
        List<CatVo> categoryList = categoryService.findCategoryList();

        /**
         *      封装所有数据部分
         *      前端要两个数据
         *      一个是包含了管理员id和管理员用户名的数组
         *      还有个是商品种类表包含下级商品的数组套数组的数据
         */
        Map<String, Object> data = new HashMap<>();
        data.put("adminList", adminList);
        data.put("categoryList", categoryList);
        log.debug("品牌和管理员列表方法执行成功。AdminBrandController--> catAndAdmin 方法");
        return ResponseUtil.ok(data);
    }

    /**
     * 品牌制造商列表显示,只返回没有逻辑删除的制造商
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param name  品牌商名称
     * @param id    商品 ID
     * @return
     */
    @RequiresPermissions("admin:brand:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品制造商管理"}, button = "查询商品制造商")
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestParam(value = "sort", required = false, defaultValue = "add_time") String sort,
            @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
            String name, Integer id
    ) {
        Map<String, Object> data = brandService.findByPage(page, limit, sort, order, name, id);
        return ResponseUtil.ok(data);
    }

    /**
     * 商品制造商删除
     *
     * @param brand
     * @return
     */
    @RequiresPermissions("admin:brand:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品制造商管理"}, button = "品牌制造商删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand) {
        log.debug("执行品牌删除。AdminBrandController --> delete 方法");
        try {
            brandService.delete(brand);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1, e.getMessage());
        }
        log.debug("品牌删除成功。AdminBrandController --> delete 方法");
        return ResponseUtil.ok();
    }

    /**
     * 品牌修改。
     *
     * @param brand
     * @return
     */
    @RequiresPermissions("admin:brand:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品制造商管理"}, button = "品牌制造商修改")
    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand brand) {
        log.debug("开始执行品牌修改");
        brandService.update(brand);
        log.debug("品牌修改执行完毕");
        return ResponseUtil.ok(brand);
    }


    /**
     * 新增商品制造商
     * @param categoryIds
     * @return
     */
    @RequiresPermissions("admin:brand:insert")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品制造商管理"}, button = "商品制造商增加")
    @PostMapping("/create")
    public Object create(@RequestBody Map<String,Object>categoryIds) {
//        前端传了11个参数，用brand接收了 10 个，用brandVo收了 1 个
        log.debug("开始执行新增品牌");
        DtsBrand data = brandService.insert(categoryIds);

//        返回前端所需要的刚新增的品牌商信息
        log.debug("新增品牌执行完毕");
        return ResponseUtil.ok(data);
//        return ResponseUtil.ok();
    }

}
